// 秒转换为时分秒hh:mm:ss。time为总秒数格式如：1000
function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}
//经纬度
function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}

function timeFormat(date, form) {
	try {
		date.getTime()
	} catch (e) {
		if (isNaN(date) && !isNaN(Date.parse(date))) {
			date = new Date(date);
		}else{
			return date;
		}
	}
	//获取时间戳的方法：new Date()).valueOf();
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	if(month<10){month="0"+month};
	var day = date.getDate();
	if(day<10){day="0"+day};
	var hour = "00";
	var minute = "00";
	var second = "00";
	try {
		hour = ss.getHours();
		if(hour<10){hour="0"+hour};
		minute = ss.getMinutes();
		if(minute<10){minute="0"+minute};
		second = ss.getSeconds();
		if(minute<10){minute="0"+minute};
	} catch (e) {
		// date = new Date(year, month - 1, date, hour, minute, second);
	}
	let backDate = "";
	switch (form) {
		case undefined:
			backDate = date.valueOf(); //时间戳
			break;
		case "yyyy-mm-dd":
			backDate = year + "-" + month + "-" + day;
			break;
		case "yyyy-mm-dd hh:mm":
			backDate = year + "-" + month + "-" + day + " " + hour + ":" + minute;
			break;
		case "yyyy-mm-dd hh:mm:ss":
			backDate = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
			break;
		case "月日":
			backDate = month + "月" + day + "日";
			break;
		case "年月日":
			backDate = year + "年" + month + "月" + day + "日";
			break;
		case "年月日时分":
			backDate = year + "年" + month + "月" + day + "日" + " " + hour + "点" + minute + "分";
			break;
		case "年月日时分秒":
			backDate = year + "年" + month + "月" + day + "日" + " " + hour + "点" + minute + "分" + second + "秒";
			break;
		default:
			backDate = year + "-" + month + "-" + day;
			break;
	}
	return backDate;


}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	//判断时间与现状隔多久，并转化成汉字输出，milliseconds：传入时间的时间戳（格式如：100000）
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		let sss = Date.now();
		let ssss = date.getTime();
		var diff = sss - ssss;
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};

function dateFormat(date) {
    return new Date(+new Date(date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
};
 function dateDayFormat(date) {
	 var da=date.replace('.000+0000', '')
	 var ab=new Date(+new Date(da) + 8 * 3600 * 1000)
	 return timeFormat(ab, "yyyy-mm-dd");
    // return new Date(+new Date(da) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').slice(0, 10);
}

function dateDayTimeFormat(date) {
	 var da=date.replace('.000+0000', '')
	 var ab=new Date(+new Date(da) + 8 * 3600 * 1000)
	 return timeFormat(ab, "yyyy-mm-dd hh:mm");
    // return new Date(+new Date(da) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').slice(0, 10);
}


// function dateDayFormat(date) {
// 	var ab = new Date()
// 	console.log('ab',ab)
// 	var bb =new Date(+new Date(ab) + 24 * 3600 * 1000)
// 	console.log('bb',bb)
//     return bb.toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').slice(0, 10);
// }
// function dateDayFormat(date) {
	
// 	date=date.replace(/T/g, ' ');
// 	console.log('date:',date)
// 	let ddd=new Date(date).getTime() + 8 * 3600 * 1000;
// 	let zzz=new Date(ddd);
// 	let yyy=zzz.toISOString();
// 	let xxx=yyy.replace(/T/g, ' ');
// 	let ccc=xxx.replace(/\.[\d]{3}Z/, '');
// 	console.log('zzz=ddd+8h:',zzz)
// 	console.log('yyy:',yyy)
// 	console.log('xxx:',xxx)
// 	console.log('ccc:',ccc)
// 	let aaa=new Date(date);
//     return new Date(ddd).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
// }

module.exports = {
	formatTime: formatTime,
	formatLocation: formatLocation,
	timeFormat: timeFormat,
	dateUtils: dateUtils,
	dateFormat:dateFormat,
	dateDayFormat:dateDayFormat
}
